package com.e3.core.base;

import java.io.Serializable;
import java.util.Date;

import com.e3.sysmg.security.server.UserHelper;

public class SessionContext implements Serializable {

	private static final long serialVersionUID = 2808191486374141479L;

	private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
	
	private static boolean isDeveloping = false;
	
	public static boolean isDeveloping() {
		return isDeveloping;
	}

	public static void setDeveloping(boolean isDeveloping) {
		SessionContext.isDeveloping = isDeveloping;
	}

	public static Session getSession(){
		Session session = threadLocal.get();
		if(session==null){
			if(isDeveloping()){
				session = createDebugSession();
			}
			else{
				session = new Session();	
			}
			setSession(session);
		}
		return session;
	}
	
	public static void setSession(Session session){
		threadLocal.set(session);
	}
	
	public static long getCorpID() {
		return getSession().getCorpID();
	}

	public static void setCorpID(long corpID) {
		getSession().setCorpID(corpID);
	}
	
	public static String getCorpCode() {
		return getSession().getCorpCode();
	}

	public static void setCorpCode(String corpCode) {
		getSession().setCorpCode(corpCode);
	}

	public static String getCorpName() {
		return getSession().getCorpName();
	}

	public static void setCorpName(String corpName) {
		getSession().setCorpName(corpName);
	}
	
	public static long getUserID() {
		return getSession().getUserID();
	}

	public static void setUserID(long userID) {
		getSession().setUserID(userID);
	}

	public static String getUserCode() {
		return getSession().getUserCode();
	}
	
	public static String getUserName(){
		return getSession().getUserName();
	}

	public static void setUserCode(String userCode) {
		getSession().setUserCode(userCode);
	}

	public static Date getLoginTime() {
		return getSession().getLoginTime();
	}

	public static void setLoginTime(Date loginTime) {
		getSession().setLoginTime(loginTime);
	}

	public static void remove(){
		//setDeveloping(false);
		threadLocal.remove();
	}
	
	public static Session createDebugSession(){
		Session session = new Session();
		
		session.setCorpID(UserHelper.getLocalCorpId());
		session.setCorpCode(UserHelper.getLocalCorpCode());
		session.setCorpName(UserHelper.getLocalCorpName());
		
		session.setUserID(UserHelper.getSuperAdminId());
		session.setUserCode(UserHelper.getSuperAdminCode());
		session.setUserName(UserHelper.getSuperAdminName());
		
		session.setLoginAddress("127.0.0.1");
		return session;
	}

	public static boolean isSuperAdimin(){
		String code = getUserCode();
		if(code == null){
			code = "";
		}
		if(code.equalsIgnoreCase(UserHelper.getSuperAdminCode())){
//			System.err.println(code +"is super admin");
			return true;
		}
//		System.err.println(code+" is not super admin");
		return false;
	}
	
	public static boolean isLocalCorp(){
		String code = getCorpCode();
		if(code == null){
			code = "";
		}
		if(code.equalsIgnoreCase(UserHelper.getLocalCorpCode())){
			return true;
		}
		return false;
	}
}
